package com.itheima.behavior.repository;

import com.itheima.model.behavior.pojos.BookPopularity;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 图书热度数据仓库
 */
@Repository
public interface BookPopularityRepository extends MongoRepository<BookPopularity, String> {

    /**
     * 根据图书ID查询热度信息
     */
    Optional<BookPopularity> findByBookId(Integer bookId);

    /**
     * 查询热门图书列表（按热度分数倒序）
     */
    @Query(value = "{}", sort = "{'popularity_score': -1}")
    List<BookPopularity> findTopBooks(Pageable pageable);

    /**
     * 查询所有图书热度信息（按热度分数倒序）
     */
    List<BookPopularity> findAllByOrderByPopularityScoreDesc();

    /**
     * 检查图书热度信息是否存在
     */
    boolean existsByBookId(Integer bookId);

    /**
     * 删除图书热度信息
     */
    void deleteByBookId(Integer bookId);
}

